#include "settingsdialog.h"

settingsDialog::settingsDialog()
{
    setModal(true);
    setGeometry(200,200,600,200);
    setWindowTitle("Settings");

    defBrowser = CHROME;
    initPaths();

    txtIEPath = new QLineEdit();
    txtFireFoxPath = new QLineEdit();
    txtChromePath = new QLineEdit();
    txtOperaPath = new QLineEdit();

    txtIEPath->setText(IEPath);
    txtFireFoxPath->setText(FireFoxPath);
    txtChromePath->setText(ChromePath);
    txtOperaPath->setText(OperaPath);

    lblIEPath = new QLabel("Internet Explorer path (iexplore.exe)");
    lblFireFoxPath = new QLabel("Firefox path (firefox.exe)");
    lblChromePath = new QLabel("Chrome path (chrome.exe)");
    lblOperaPath = new QLabel("Opera path (opera.exe)");

    radIEPath = new QRadioButton();
    radFireFoxPath = new QRadioButton();
    radChromePath = new QRadioButton();
    radOperaPath = new QRadioButton();

    radIEPath->setAutoExclusive(true);
    radFireFoxPath->setAutoExclusive(true);
    radChromePath->setAutoExclusive(true);
    radOperaPath->setAutoExclusive(true);


    radIEPath->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Fixed);
    radFireFoxPath->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Fixed);
    radChromePath->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Fixed);
    radOperaPath->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Fixed);

    radChromePath->setChecked(true);

    buttOK = new QPushButton("Ok");
    buttCancel = new QPushButton("Cancel");
    buttOK->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    buttCancel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);


    QGridLayout* grid = new QGridLayout();

    grid->addWidget(lblIEPath,0,0);
    grid->addWidget(txtIEPath,0,1);
    grid->addWidget(radIEPath,0,2);

    grid->addWidget(lblFireFoxPath,1,0);
    grid->addWidget(txtFireFoxPath,1,1);
    grid->addWidget(radFireFoxPath,1,2);

    grid->addWidget(lblChromePath,2,0);
    grid->addWidget(txtChromePath,2,1);
    grid->addWidget(radChromePath,2,2);

    grid->addWidget(lblOperaPath,3,0);
    grid->addWidget(txtOperaPath,3,1);
    grid->addWidget(radOperaPath,3,2);

    grid->addWidget(buttOK,4,0,1,1,Qt::AlignLeft);

    grid->addWidget(buttCancel,4,2,1,1,Qt::AlignRight);
    setLayout(grid);

    connect(buttOK,SIGNAL(clicked()),this,SLOT(savePaths()));
    connect(buttCancel,SIGNAL(clicked()),this,SLOT(close()));

    connect(radChromePath,SIGNAL(toggled(bool)),this,SLOT(setDef(bool)));
    connect(radIEPath,SIGNAL(toggled(bool)),this,SLOT(setDef(bool)));
    connect(radFireFoxPath,SIGNAL(toggled(bool)),this,SLOT(setDef(bool)));
    connect(radOperaPath,SIGNAL(toggled(bool)),this,SLOT(setDef(bool)));


}

void
settingsDialog::initPaths()
{
    QString appData = QString(getenv("LOCALAPPDATA"));
    QString programFiles = QString(getenv("PROGRAMFILES"));
    qDebug()<<appData;
    qDebug()<<programFiles;
    ChromePath = appData + QString("\\Google\\Chrome\\Application\\chrome.exe");
    FireFoxPath = programFiles + QString("\\Mozilla Firefox\\firefox.exe");
    IEPath = programFiles + QString("\\Internet Explorer\\iexplore.exe");
    OperaPath = ChromePath; //Dont have opera.

    Browsers[CHROME] = &ChromePath;
    Browsers[FIREFOX] = &FireFoxPath;
    Browsers[IE] = &IEPath;
    Browsers[OPERA] = &OperaPath;
}

void
settingsDialog::savePaths()
{
    ChromePath = txtChromePath->text();
    FireFoxPath = txtFireFoxPath->text();
    IEPath = txtIEPath->text();
    OperaPath = txtOperaPath->text();
    close();
}

void
settingsDialog::setDef(bool k)
{
    if(radIEPath->isChecked()) defBrowser = IE;
    else if(radFireFoxPath->isChecked()) defBrowser = FIREFOX;
    else if(radChromePath->isChecked()) defBrowser = CHROME;
    else if(radOperaPath->isChecked()) defBrowser = OPERA;
}
